package java_0619_FileReader;

import org.junit.jupiter.api.Test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FIleReader_ {
    public static void main(String[] args) {

    }
    @Test
    public void readFile01(){
        String filePath = "e:\\hello.txt";
        FileReader fileReader = null;
        int data = 0;

        try {
            fileReader = new FileReader(filePath);
            //循环读取 使用read
            while((data = fileReader.read())!= -1){
                System.out.print( (char) data);

            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if (fileReader != null){
                try {
                    fileReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }
    }

    //字符数组读取文件
    @Test
    public void readFile02(){
        String filePath = "e:\\hello.txt";
        FileReader fileReader = null;
        int readlen = 0;
        char[] buf = new char[8];

        try {
            fileReader = new FileReader(filePath);
            //循环读取 使用read(buf) ,返回的是实际读取到的字符数
            while((readlen = fileReader.read(buf))!= -1){
                System.out.print(new String(buf,0,readlen));

            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if (fileReader != null){
                try {
                    fileReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }
    }
}
